<template>
	<div class="sdt" ref="echartDom"></div>
</template>

<script setup lang="ts">

import { inject, ref, shallowRef, onMounted, onUnmounted, watch, toRaw, watchEffect, defineEmits } from "vue";

// 接收参数
const props = defineProps({
	housingSupplyStatsData: {
		type: Object,
		default: () => { },
	},
});
// 数据定义
const echartDom = ref<null | HTMLDivElement>(null);
const myChart = shallowRef<null | any>(null);
const echarts = inject<any>("echarts");
const emit = defineEmits(['scatterDiagramMagfiil'])
var CLUSTER_COUNT = 6;
var DIENSIION_CLUSTER_INDEX = 2;
// 字体自适应方法
const pxToRem = (px: number) => {
	const scale = document.documentElement.clientWidth / 1920;
	return Math.round(px * Math.min(scale, 3));
};
// echarts初始化渲染
let COLOR_ALL = props.housingSupplyStatsData.colors//颜色
let legendStyle = props.housingSupplyStatsData.legendStyle//图例
let gridStyle = props.housingSupplyStatsData.gridConfig//网格
let seriesData = props.housingSupplyStatsData.seriesData//serires数据
let seriesStyle = props.housingSupplyStatsData.seriesStyle//serires配置
let chartsData = props.housingSupplyStatsData.chartsData
const getEchart = () => {
	if (echartDom.value) {
		myChart.value = echarts.init(echartDom.value);
		// 提示框文字样式
		const tooltipTextStyle = {
			color: "#fff",
			fontSize: pxToRem(14),
		};

		// 提示框背景
		const tooltipBackgroundColor = "rgba(47, 99, 205, 0.5)";
		// 图例配置
		// 网格区域
		const gridConfig = {
			left: gridStyle.left,
			right: gridStyle.right,
			bottom: gridStyle.bottom,
			top: gridStyle.top,
			containLabel: true,
		};
		const legendConfig = {
			show: true,
			icon: 'rect',
			left: 'center',
			top: pxToRem(5),
			itemWidth: pxToRem(10),
			itemHeight: pxToRem(10),
			itemGap: pxToRem(5),
			textStyle: {
				color: "#fff",
				fontSize: pxToRem(12),
				textStyle: {
					color: "#fff",
					fontSize: pxToRem(12),
					rich: {},
					padding: [0, 0, pxToRem(-3), 0],
				},
				data: chartsData,
			},
		};
		let seriesConfig = seriesData.map((item, index) => {
			return {
				name: chartsData[index].name,
				type: 'scatter',
				symbolSize: pxToRem(8),
				data: toRaw(seriesData[index]),

			}
		})

		const option = {
			tooltip: {
				// trigger: "axis",
				formatter: function (params) {
						return(`${params.seriesName}<br/><span>单价: </span><span>${params.value[0]}元</span><br/><span>面积：</span><span>${params.value[1]}m²</span><br/>`)
				},
				axisPointer: {
					type: "line",
					cursor: "pointer" // 设置鼠标移入时显示小手指样式
				},
				position: function (point: any[], params: any, dom: any, rect: any, size: any) {
					return [point[1], '10%']; // 示例：将 tooltip 定位在鼠标的水平位置和视窗顶部 10% 的位置
				},
				textStyle: tooltipTextStyle,
				backgroundColor: tooltipBackgroundColor,
				borderColor: '#66FFFF', // 设置边框颜色为红色
				borderWidth: 1, // 设置边框宽度
			},

			grid: gridConfig,
			xAxis: {
				name: '价格',
				axisLabel: {
					fontSize: pxToRem(12),
					color: 'white',
					align: 'center',
				},
				nameTextStyle: {
					color: "white",
					fontSize: pxToRem(12),
					padding: [0, 0, 50, -10],
				},
				axisTick: {
					show: false // 不显示坐标轴刻度线
				},
				splitLine: {
					show: false,
				},
				axisLine: {
					lineStyle: {
						color: "#E0E6F1",
					}
				}
			},
			yAxis: {
				name: '面积',
				axisLine: {
					show: false // 隐藏Y轴线
				},
				axisTick: {
					show: false // 不显示坐标轴刻度线
				},
				splitLine: {
					lineStyle: {
						type: "rgba(255, 255, 255, 1)",
					},
				},
				axisLabel: {
					fontSize: pxToRem(14),
					color: 'white',
				},
				nameTextStyle: {
					color: "white",
					fontSize: pxToRem(14),
					padding:[0,0,0,pxToRem(-30)]
				},
			},
			legend: legendConfig,
			series: seriesConfig
		};
		myChart.value.setOption(option,true);
		myChart.value.on("click", (params) => {
			
			emit('scatterDiagramMagfiil', props.housingSupplyStatsData)
		});
	}
};
const updateChart = () => {
	if (myChart.value) {
		myChart.value.dispose(); // 销毁旧图表实例
	}

 COLOR_ALL = props.housingSupplyStatsData.colors//颜色
legendStyle = props.housingSupplyStatsData.legendStyle//图例
 gridStyle = props.housingSupplyStatsData.gridConfig//网格
seriesData = props.housingSupplyStatsData.seriesData//serires数据
 seriesStyle = props.housingSupplyStatsData.seriesStyle//serires配置
 chartsData = props.housingSupplyStatsData.chartsData
	getEchart();
};

watchEffect(() => {
	updateChart();
});

onMounted(() => {
	getEchart();
	window.addEventListener("resize", () => {
		if (myChart.value) {
			myChart.value.dispose(); // 销毁旧图表实例
		}
		getEchart(); // 创建新的图表实例
	});
});

onUnmounted(() => {
	window.removeEventListener("resize", () => {
		if (myChart.value) {
			myChart.value.dispose(); // 在组件销毁时也需要销毁图表实例
		}
	});
});
</script>

<style scoped lang="less">
.sdt {
	width: 100%;
	height: 100%;
}

.echarts-background {
	background-color: transparent !important;
}
</style>
